package be.jhd.repository.nexus;

import be.jhd.repository.Artifact;
import be.jhd.repository.UrlBuilder;

public class UrlBuilderImpl implements UrlBuilder{
	
	private final DownloaderConfig config;
	private final RepositoryResolver resolver;

	public UrlBuilderImpl(DownloaderConfig config, RepositoryResolver resolver){
		this.config = config;
		this.resolver = resolver;
	}

	public String buildUrl(Artifact artifact){
		StringBuilder builder = new StringBuilder(config.getRepositoryUrl())
		.append("?g=").append(artifact.getGroup())
		.append("&a=").append(artifact.getArtifact())
		.append("&v=").append(artifact.getVersion());
		if(artifact.isSnapshot()){
			builder.append(Artifact.SNAPSHOT);
		}
		builder.append("&r=").append(resolver.findRepository(artifact))
		.append("&p=").append(artifact.getPackaging())
		.append("&c=").append(artifact.getClassifier()).toString();
		
		return builder.toString();
	}
}
